//
//  DNPayerControl.m
//  02player
//
//  Created by lck on 16/4/10.
//  Copyright © 2016年 Devin.LJ. All rights reserved.
//

#import "DNPayerControl.h"
#import "Masonry.h"

@interface DNPayerControl  ()
/// 播放
@property (nonatomic, strong) UIButton *startBtn;
/// 全屏
@property (nonatomic, strong) UIButton *fullScreenBtn;
/// 锁屏
@property (nonatomic, strong) UIButton *lockBtn;
/// 返回
@property (nonatomic, strong) UIButton *backBtn;
/// 重播
//@property (nonatomic, strong) UIButton *repeatBtn;
/// 缓存
@property (nonatomic, strong) UIButton *downLoadBtn;
/// 当前播放时间
@property (nonatomic, strong) UILabel *currentTimeLabel;
/// 总时间
@property (nonatomic, strong) UILabel *totalTimeLabel;
/// 快进快退
@property (nonatomic, strong) UILabel *horizontalLabel;
/// BottomView
@property (nonatomic, strong) UIImageView *bottomImageView;
/// TopView
@property (nonatomic, strong) UIImageView *topImageView;
/// 滑杆
@property (nonatomic, strong) UISlider *videoSlider;
/// 缓冲进度条
@property (nonatomic, strong) UIProgressView *progressView;
/// 系统菊花
@property (nonatomic, strong) UIActivityIndicatorView *activity;

@end

@implementation DNPayerControl



- (instancetype)init {
 
    if (self = [super init]) {
        [self addSubviews];
    }
    return self;
}


- (void)addSubviews {

    [self addSubview:self.topImageView];
    [self addSubview:self.bottomImageView];
    [self.bottomImageView addSubview:self.startBtn];
    [self.bottomImageView addSubview:self.progressView];
    [self.bottomImageView addSubview:self.currentTimeLabel];
    [self.bottomImageView addSubview:self.totalTimeLabel];
    [self.bottomImageView addSubview:self.videoSlider];
    [self.bottomImageView addSubview:self.fullScreenBtn];
    [self.topImageView addSubview:self.downLoadBtn];
    
    
    [self addSubview:self.lockBtn];
    [self addSubview:self.backBtn];
    [self addSubview:self.activity];
//    [self addSubview:self.repeatBtn];
    [self addSubview:self.horizontalLabel];
    //添加约束
    [self makeSubViewsConstraints];

}


- (void)makeSubViewsConstraints {
    
    //顶部
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.equalTo(self);
        make.height.mas_equalTo(80);
    }];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(15);
        make.top.equalTo(self.mas_top).offset(5);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.downLoadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.trailing.equalTo(self.topImageView.mas_trailing).offset(-10);
        make.centerY.equalTo(self.backBtn.mas_centerY);
    }];
   //底部
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.bottomImageView.mas_leading).offset(5);
        make.bottom.equalTo(self.bottomImageView.mas_bottom).offset(-5);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.startBtn.mas_trailing).offset(2);
        make.centerY.equalTo(self.startBtn.mas_centerY);
    }];
    
    [self.fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(30);
        make.trailing.equalTo(self.bottomImageView.mas_trailing).offset(-5);
        make.centerY.equalTo(self.startBtn.mas_centerY);
    }];
    
    [self.totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.fullScreenBtn.mas_leading).offset(-2);
        make.centerY.equalTo(self.startBtn.mas_centerY);;
    }];
    
   [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.currentTimeLabel.mas_trailing).offset(8);
        make.trailing.equalTo(self.totalTimeLabel.mas_leading).offset(-8);
        make.centerY.equalTo(self.startBtn.mas_centerY);
    }];
    
    [self.videoSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.currentTimeLabel.mas_trailing).offset(8);
        make.trailing.equalTo(self.totalTimeLabel.mas_leading).offset(-8);
        make.centerY.equalTo(self.currentTimeLabel.mas_centerY).offset(-0.25);
    }];
    
   

   
    
    
//    [self.lockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.mas_leading).offset(15);
//        make.centerY.equalTo(self.mas_centerY);
//        make.width.height.mas_equalTo(40);
//    }];
    
//    [self.horizontalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(160);
//        make.height.mas_equalTo(40);
//        make.center.equalTo(self);
//    }];
    
//    [self.activity mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(self);
//    }];
    
//    [self.repeatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(self);
//    }];
    
}

///MARK:- 重置Control
- (void)resetControlView {
    self.videoSlider.value     = 0;
    self.progressView.progress = 0;
    self.currentTimeLabel.text = @"00:00";
    self.totalTimeLabel.text   = @"00:00";
    self.backgroundColor = [UIColor clearColor];
}

- (void)showControlView {
    self.topImageView.alpha    = 1;
    self.bottomImageView.alpha = 1;
    self.lockBtn.alpha         = 1;
}

- (void)hideControlView {
    self.topImageView.alpha    = 0;
    self.bottomImageView.alpha = 0;
    self.lockBtn.alpha         = 0;
}


- (UIButton *)backBtn {
    if (_backBtn == nil) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"hd_icnav_back_dark"] forState:UIControlStateNormal];
    }
    return _backBtn;
}

- (UIImageView *)topImageView {
    if (_topImageView == nil) {
        _topImageView = [[UIImageView alloc] init];
        _topImageView.userInteractionEnabled = YES;
        _topImageView.image = [UIImage imageNamed:@"top_shadow"];
    }
    return _topImageView;
}

- (UIImageView *)bottomImageView {
    if (_bottomImageView == nil) {
        _bottomImageView = [[UIImageView alloc] init];
        _bottomImageView.userInteractionEnabled = YES;
        _bottomImageView.image = [UIImage imageNamed:@"bottom_shadow"];
    }
    return _bottomImageView;
}

- (UIButton *)lockBtn {
    if (_lockBtn == nil) {
        _lockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lockBtn setImage:[UIImage imageNamed:@"pws_icon"] forState:UIControlStateNormal];
        [_lockBtn setImage:[UIImage imageNamed:@"pws_icon"] forState:UIControlStateSelected];
    }
    return _lockBtn;
}

- (UIButton *)startBtn {
    if (_startBtn == nil) {
        _startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startBtn setImage:[UIImage imageNamed:@"download_start"] forState:UIControlStateNormal];
        [_startBtn setImage:[UIImage imageNamed:@"download_stop"] forState:UIControlStateSelected];
    }
    return _startBtn;
}

- (UILabel *)currentTimeLabel {
    if (_currentTimeLabel == nil) {
        _currentTimeLabel           = [[UILabel alloc] init];
        _currentTimeLabel.textColor = [UIColor whiteColor];
        _currentTimeLabel.font      = [UIFont systemFontOfSize:12.0f];
        _currentTimeLabel.text = @"00:00";
    }
    return _currentTimeLabel;
}

- (UIProgressView *)progressView {
    if (_progressView == nil) {
        _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.progressTintColor = [UIColor darkGrayColor];
//        [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
        
        _progressView.trackTintColor = [UIColor clearColor];
        _progressView.progress = 0.5;
    }
    return _progressView;
}

- (UISlider *)videoSlider {
    
    if (_videoSlider == nil) {
        _videoSlider = [[UISlider alloc] init];
        [_videoSlider setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
        _videoSlider.minimumTrackTintColor = [UIColor whiteColor];
        _videoSlider.maximumTrackTintColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.6];
    }
    return _videoSlider;
}

- (UILabel *)totalTimeLabel {
    if (_totalTimeLabel == nil) {
        _totalTimeLabel           = [[UILabel alloc] init];
        _totalTimeLabel.textColor = [UIColor whiteColor];
        _totalTimeLabel.font      = [UIFont systemFontOfSize:12.0f];
        _totalTimeLabel.text = @"00:00";
    }
    return _totalTimeLabel;
}

- (UIButton *)fullScreenBtn {
    if (_fullScreenBtn == nil) {
        _fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fullScreenBtn setImage:[UIImage imageNamed:@"chase_roomplayer_fullscreen_icon"] forState:UIControlStateNormal];
        [_fullScreenBtn setImage:[UIImage imageNamed:@"chase_roomplayer_fullscreen_icon"] forState:UIControlStateSelected];
    }
    return _fullScreenBtn;
}

- (UILabel *)horizontalLabel {
    if (!_horizontalLabel) {
        _horizontalLabel                 = [[UILabel alloc] init];
        _horizontalLabel.textColor       = [UIColor whiteColor];
        _horizontalLabel.textAlignment   = NSTextAlignmentCenter;
        _horizontalLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];
    }
    return _horizontalLabel;
}

- (UIActivityIndicatorView *)activity
{
    if (!_activity) {
        _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    }
    return _activity;
}

//- (UIButton *)repeatBtn
//{
//    if (!_repeatBtn) {
//        _repeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_repeatBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    }
//    return _repeatBtn;
//}

- (UIButton *)downLoadBtn
{
    if (!_downLoadBtn) {
        _downLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downLoadBtn setImage:[UIImage imageNamed:@"ictab_misc"] forState:UIControlStateNormal];
        [_downLoadBtn setImage:[UIImage imageNamed:@"ictab_misc"] forState:UIControlStateDisabled];
    }
    return _downLoadBtn;
}



@end
